import { _decorator, Component, Size, size, Sprite, SpriteAtlas, UITransform, v2, Widget } from 'cc';
import Ab_GameAssets_bA from '../manager/game.assets';
const { ccclass, property } = _decorator;

/** 砖块的设计尺寸 */
const Ab_TILE_DESIGN_SIZE_bA = {
	block: size(116, 116),
	background: size(120, 132),
	icon: size(90, 90)
}

@ccclass('Ab_Tile_bA')
export class Ab_Tile_bA extends Component {
	@property(UITransform)
	private Ab_uitrans_bA: UITransform = null;
	@property(Sprite)
	private Ab_sprite_bA: Sprite = null;
	@property(SpriteAtlas)
	private Ab_atlas_bA: SpriteAtlas = null;
	@property(UITransform)
	private Ab_ui_background_bA: UITransform = null;
	@property(UITransform)
	private Ab_ui_shade_bA: UITransform = null;
	@property(UITransform)
	private Ab_ui_icon_bA: UITransform = null;

	private Ab_id_bA: string;
	private Ab_light_bA: boolean = false;
	private Ab_block_size_bA = Ab_TILE_DESIGN_SIZE_bA.block.clone();

	public get Ab_ID_bA(){
		return this.Ab_id_bA;
	}
	public get Ab_Light_bA(){
		return this.Ab_light_bA;
	}
	public get Ab_active_bA(){
		return this.node && this.node.active && this.node.isValid;
	}
	public get Ab_width_bA(){
		return this.Ab_block_size_bA.width;
	}
	public get Ab_height_bA(){
		return this.Ab_block_size_bA.height;
	}

	protected onEnable(){
		this.Ab_updateSize_bA();
		this.Ab_updateSprite_bA();
		this.Ab_ui_shade_bA.node.active = !this.Ab_Light_bA;
	}

	public Ab_init_bA(id: string, width: Size|number, height?: number){
		this.Ab_setID_bA(id);
		this.Ab_setSize_bA(width, height);
	}

	public Ab_setLight_bA(light: boolean){
		this.Ab_light_bA = light;

		if(this.Ab_active_bA) this.Ab_ui_shade_bA.node.active = !light;
	}

	public Ab_setSize_bA(width: Size|number, height?: number){
		if(width instanceof Size){
			height = width.height;
			width = width.width;
		}else if(!(width>0)) width = Ab_TILE_DESIGN_SIZE_bA.block.width;

		if(!(height>0)) height = width;

		let scale = Math.min(width / Ab_TILE_DESIGN_SIZE_bA.block.width, height / Ab_TILE_DESIGN_SIZE_bA.block.height);
		this.Ab_block_size_bA = size(Ab_TILE_DESIGN_SIZE_bA.block.width * scale, Ab_TILE_DESIGN_SIZE_bA.block.height * scale);

		this.Ab_updateSize_bA();
	}

	public Ab_setID_bA(id: string){
		this.Ab_id_bA = id;
		this.Ab_updateSprite_bA();
	}

	private Ab_offset_bA = v2();
	private Ab_bg_offset_bA = v2();
	private Ab_bg_center_bA = v2();
	public Ab_getBackgroundPosition_bA(width?: Size|number, height?: number){
		if(width instanceof Size){
			height = width.height;
			width = width.width;
		}else if(!(width>0)) width = this.Ab_uitrans_bA.width;

		if(!(height>0)) height = width;
		let scale = Math.min(width / Ab_TILE_DESIGN_SIZE_bA.block.width, height / Ab_TILE_DESIGN_SIZE_bA.block.height);

		[this.Ab_offset_bA, this.Ab_bg_offset_bA] = [this.Ab_uitrans_bA, this.Ab_ui_background_bA]
		.map(ui=>v2((ui.anchorX-0.5) * ui.width, (ui.anchorY-0.5) * ui.height));
		this.Ab_bg_center_bA = v2(this.Ab_ui_background_bA.node.position.x-this.Ab_bg_offset_bA.x, this.Ab_ui_background_bA.node.position.y-this.Ab_bg_offset_bA.y);
		this.Ab_bg_center_bA.add(this.Ab_offset_bA);

		return this.Ab_bg_center_bA.multiplyScalar(scale);
	}

	private Ab_updateSize_bA(){
		if(!this.Ab_active_bA) return void 0;

		this.Ab_uitrans_bA.setContentSize(this.Ab_block_size_bA);

		let scale = this.Ab_block_size_bA.width / Ab_TILE_DESIGN_SIZE_bA.block.width;

		[this.Ab_ui_background_bA, this.Ab_ui_shade_bA].forEach(ui=>{
			ui.setContentSize(Ab_TILE_DESIGN_SIZE_bA.background.width * scale, Ab_TILE_DESIGN_SIZE_bA.background.height * scale);
			let wgt = ui.node.getComponent(Widget);
			if(wgt) wgt.updateAlignment();
		});

		let ratio = Ab_Tile_bA.Ab_getRatio_bA(size(Ab_TILE_DESIGN_SIZE_bA.icon.width * scale, Ab_TILE_DESIGN_SIZE_bA.icon.height * scale), this.Ab_sprite_bA.spriteFrame.originalSize);
		this.Ab_ui_icon_bA.setContentSize(this.Ab_sprite_bA.spriteFrame.originalSize.width * ratio, this.Ab_sprite_bA.spriteFrame.originalSize.height * ratio);
	}

	private Ab_updateSprite_bA(){
		if(!this.Ab_active_bA) return void 0;

		if(this.Ab_id_bA && this.Ab_id_bA!=this.Ab_sprite_bA.spriteFrame.name){
			let sframe = this.Ab_id_bA=='00' ? Ab_GameAssets_bA.Ab_getCounTexture_bA(0) : this.Ab_atlas_bA.getSpriteFrame(this.Ab_id_bA);
			this.Ab_sprite_bA.spriteFrame = sframe;
		}

		let scale = this.Ab_block_size_bA.width / Ab_TILE_DESIGN_SIZE_bA.block.width;
		let ratio = Ab_Tile_bA.Ab_getRatio_bA(size(Ab_TILE_DESIGN_SIZE_bA.icon.width * scale, Ab_TILE_DESIGN_SIZE_bA.icon.height * scale), this.Ab_sprite_bA.spriteFrame.originalSize);
		this.Ab_ui_icon_bA.setContentSize(this.Ab_sprite_bA.spriteFrame.originalSize.width * ratio, this.Ab_sprite_bA.spriteFrame.originalSize.height * ratio);
	}

	/**
	 * 获取填充物在容器内的内切缩放比例
	 * @param contianer
	 * @param inner
	 */
	private static Ab_getRatio_bA(contianer: Size, inner: Size){
		let [rc, ri] = [contianer, inner].map(s=>s.width / s.height);
		return ri>rc ? contianer.width / inner.width : contianer.height / inner.height;
	}

	/**
	 * 计算砖块的尺寸
	 * @param width
	 * @param height
	 */
	public static Ab_calculateSizeByBlock_bA(width: number, height: number){
		let ratio = Ab_Tile_bA.Ab_getRatio_bA(size(width, height), Ab_TILE_DESIGN_SIZE_bA.block);
		let [block, background, icon] = [Ab_TILE_DESIGN_SIZE_bA.block, Ab_TILE_DESIGN_SIZE_bA.background, Ab_TILE_DESIGN_SIZE_bA.icon].map(s=>size(s.width * ratio, s.height * ratio));
		return {block, background, icon};
	}

	/**
	 * 计算砖块的背景尺寸
	 * @param width
	 * @param height
	 */
	public static Ab_calculateSizeByBackground_bA(width: number, height: number){
		let ratio = Ab_Tile_bA.Ab_getRatio_bA(size(width, height), Ab_TILE_DESIGN_SIZE_bA.background);
		let [block, background, icon] = [Ab_TILE_DESIGN_SIZE_bA.block, Ab_TILE_DESIGN_SIZE_bA.background, Ab_TILE_DESIGN_SIZE_bA.icon].map(s=>size(s.width * ratio, s.height * ratio));
		return {block, background, icon};
	}

}

